<?php
/*
	本插件：$this->model('worker')->方法名();
	其他地方调用：model('jy_scrm/worker')->方法名();
*/
namespace jy_scrm;
defined('BY_JYA') or exit('error');
class worker extends \plugin{
	public function get_userid($workerid){
		$userid=pdo_getcolumn('jy_scrm_worker',array('id'=>$workerid,'uniacid'=>$_SESSION['uniacid']),'userid');
		return $userid;
	}
	public function get_workerid($userid,$operuserid=''){
		$workerid=pdo_getcolumn('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'userid'=>$userid,'uniacid'=>$_SESSION['uniacid']),'id');
		if(!$workerid){
			$workerid=pdo_getcolumn('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'userid'=>$operuserid,'uniacid'=>$_SESSION['uniacid']),'id');
		}
		return $workerid;
	}
	public function batch($bumenid,$fetch_child=1){
		if(!$bumenid){
			exi('请传入部门ID','error');
		}
		$userlist=model('core/wxwork_userapi')->get_worker_batch($bumenid,$fetch_child);
		foreach($userlist as $item){
			$ischeck=pdo_getcolumn('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'userid'=>$item['userid'],'uniacid'=>$_SESSION['uniacid']),'id');
			if($ischeck){
				continue;
			}
			$this->create($item);
		}
	}
	public function batchinfo($bumenid,$fetch_child=1){
		if(!$bumenid){
			exi('请传入部门ID','error');
		}
		$userlist=model('core/wxwork_userapi')->get_worker_batchinfo($bumenid,$fetch_child);
		foreach($userlist as $item){
			//$ischeck=pdo_getcolumn('jy_scrm_worker',array('userid'=>$item['userid'],'uniacid'=>$_SESSION['uniacid']),'id');
			if($ischeck){
				//continue;
			}
			$this->create($item);
		}
	}
	public function create($params){
		$department=array();
		$bumenids=';';
		foreach($params['department'] as $k=>$item){
			$bumenids .=$item.';';
			$department[]=array('id'=>$item,'order'=>$params['order'][$k],'is_leader_in_dept'=>$params['is_leader_in_dept'][$k]);
		}
		$worker=array(
			'userid'=>$params['userid'],
			'name'=>$params['name'],
			'open_userid'=>$params['open_userid'],
			'department'=>ijson_encode($department),
			'bumenids'=>$bumenids,
			'position'=>$params['position'],
			'mobile'=>$params['mobile'],
			'gender'=>$params['gender'],
			'email'=>$params['email'],
			'avatar'=>$params['avatar'],
			'thumb_avatar'=>$params['thumb_avatar'],
			'telephone'=>$params['telephone'],
			'alias'=>$params['alias'],
			'status'=>$params['status'],
			'address'=>$params['address'],
			'hide_mobile'=>$params['hide_mobile'],
			'english_name'=>$params['english_name'],
			'open_userid'=>$params['open_userid'],
			'main_department'=>$params['main_department'],
			'extattr'=>ijson_encode($params['extattr']['attrs']),
			'qr_code'=>$params['qr_code'],
			'external_position'=>$params['external_position'],
			'external_profile'=>ijson_encode($params['external_profile']),
			'isout'=>$params['isout'],
		);
		$worker['isup']=1;
		$id=pdo_getcolumn('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'userid'=>$params['userid'],'uniacid'=>$_SESSION['uniacid']),'id');
		if($id){
			if(is_numeric($worker['name'])){
				unset($worker['name']);unset($worker['mobile']);
			}
			$this->pdo_update('worker',$worker,array('merchid'=>$_SESSION['onemerchid'],'userid'=>$params['userid'],'uniacid'=>$_SESSION['uniacid']));	
		}else{
			$worker['uniacid']=$_SESSION['uniacid'];
			$worker['merchid']=$_SESSION['onemerchid'];
			$worker['createtime']=$params['createtime']?:TIMESTAMP;
			$this->pdo_insert('worker',$worker);
			$id=$this->pdo_insertid();
		}
		return $id;
	}
	//更新敏感信息
	public function upinfo($params){
		$worker=array(
			'mobile'=>$params['mobile'],
			'gender'=>$params['gender'],
			'email'=>$params['email'],
			'avatar'=>$params['avatar'],
			'qr_code'=>$params['qr_code'],
			'address'=>$params['address'],
		);
		$id=pdo_getcolumn('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'userid'=>$params['userid'],'uniacid'=>$_SESSION['uniacid']),'id');
		if($id){
			$this->pdo_update('worker',$worker,array('merchid'=>$_SESSION['onemerchid'],'userid'=>$params['userid'],'uniacid'=>$_SESSION['uniacid']));	
		}else{
			$worker['userid']=$params['userid'];
			$worker['uniacid']=$_SESSION['uniacid'];
			$worker['merchid']=$_SESSION['onemerchid'];
			$worker['createtime']=$params['createtime']?:TIMESTAMP;
			$this->pdo_insert('worker',$worker);
			$id=$this->pdo_insertid();
		}
		return $id;
	}
	public function updateto($data=array()){
		model('core/wxwork_userapi')->worker_update($data);
		return true;
	}
	public function createto($data=array()){
		model('core/wxwork_userapi')->worker_create($data);
		return true;
	}
	public function delete($id=0){
		if(!$id){
			exi('请选择员工ID','error');
		}
		$userid=pdo_getcolumn('jy_scrm_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'userid');
		model('core/wxwork_userapi')->worker_delete($userid);
		return true;
	}
	public function bumen_updateto($data=array()){
		model('core/wxwork_userapi')->bumen_update($data);
		return true;
	}
	public function bumen_createto($data=array()){
		model('core/wxwork_userapi')->bumen_create($data);
		return true;
	}
	public function bumen_delete($id=0){
		if(!$id){
			exi('请选择部门ID','error');
		}
		$bumenid=pdo_getcolumn('jy_scrm_worker_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'bumenid');
		model('core/wxwork_userapi')->bumen_delete($bumenid);
		return true;
	}
	//查询企业红包记录
	public function workwxredpack($params){
		$api_url='https://api.mch.weixin.qq.com/mmpaymkttransfers/queryworkwxredpack';
		$fields=array('mch_billno');
		foreach($fields as $key){
			if(!$params[$key]){
				return error('关键参数未设置：'.$key);
			}
		}
		$post=array(
			'nonce_str'=>random(32),
			'mch_billno'=>$params['mch_billno'],//商户订单号
			'mch_id'=>$this->config['mch_id'],
			'appid'=>$this->config['appid'],
			'sign'=>$sign,
		);
		return $this->request($api_url,$post,true,true);
	}
	public function get_dimissionworker($page_id=0,$cursor='',$page_size=100){
		$content=model('core/wxwork_userapi')->get_dimissionworker_members($page_id,$cursor,$page_size);
		foreach($content['info'] as $item){
			$this->createdimission($item);
		}
		if($content['next_cursor'] && !$content['is_last']){
			$this->get_dimissionworker($page_id,$content['next_cursor'],$page_size);
		}
		return true;
	}
	public function createdimission($params){
		$data=array(
			'handover_userid'=>$params['handover_userid'],
			'external_userid'=>$params['external_userid'],
			'dimission_time'=>$params['dimission_time'],
		);
		$id=pdo_getcolumn('jy_scrm_worker_dimission',array('merchid'=>$_SESSION['onemerchid'],'handover_userid'=>$params['handover_userid'],'external_userid'=>$params['external_userid'],'uniacid'=>$_SESSION['uniacid']),'id');
		if($id){
			return $id;
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$this->pdo_insert('worker_dimission',$data);
			$id=$this->pdo_insertid();
		}		
		$this->pdo_update('worker',array('status'=>4,'dimissiontime'=>$params['dimission_time']),array('merchid'=>$_SESSION['onemerchid'],'userid'=>$params['external_userid'],'uniacid'=>$_SESSION['uniacid']));
		return $id;
	}
	public function check_contact_way($state){
		$contact_way=$this->pdo_get('contact_way',array('state'=>$state,'uniacid'=>$_SESSION['uniacid']));
		return $contact_way;
	}
	public function add_contact_way($params){
		$config_id=$this->pdo_getcolumn('contact_way',array('merchid'=>$_SESSION['onemerchid'],'config_id'=>$params['config_id'],'uniacid'=>$_SESSION['uniacid']),'config_id');
		$data=array(
			'style'=>$params['style']?:0,
			'remark'=>$params['remark'],
			'skip_verify'=>$params['skip_verify'],
			'qr_code'=>$params['qr_code'],
			'user'=>$params['user'],
			'party'=>$params['party'],
			'is_temp'=>$params['is_temp'],
			'expires_in'=>$params['expires_in'],
			'chat_expires_in'=>$params['chat_expires_in'],
			'unionid'=>$params['unionid'],
			'conclusions'=>ijson_encode($params['conclusions']),
			'status'=>$params['status'],
		);
		if($config_id){
			$this->pdo_update('contact_way',$data,array('merchid'=>$_SESSION['onemerchid'],'config_id'=>$config_id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['config_id']=$params['config_id'];
			$data['scene']=$params['scene'];
			$data['type']=$params['type'];
			$data['state']=$params['state'];
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$_SESSION['onemerchid'];
			$data['createtime']=TIMESTAMP;
			$this->pdo_insert('contact_way',$data);
		}
		return $params['config_id'];
	}
	public function get_ewm($userid,$name='',$state=''){
		$newstate=$state?:'jy_scrm|userid|'.$userid;
		$code=$this->get('contact_way',array('merchid'=>$_SESSION['onemerchid'],'type'=>1,'user'=>$userid,'state'=>$newstate,'uniacid'=>$_SESSION['uniacid']));
		if(!$code['qr_code']){
			$params=array(
				'type'=>1,
				'scene'=>2,
				'remark'=>$name.'单人员工二维码',
				'skip_verify'=>true,
				'state'=>$newstate,
				'user'=>$userid,
				'is_temp'=>false,
			);
			$res=model('core/wxwork_userapi')->add_contact_way($params,true);
			$params['config_id']=$res['config_id'];
			$params['qr_code']=$res['qr_code'];
			$this->add_contact_way($res);
			$ewm=$res['qr_code'];
		}else{
			$ewm=$code['qr_code'];
		}
		return $ewm;
	}
	public function up_userids_count($page=1){
		if($page==1){
			$this->huo_day0num=0;
			$this->huo_day7num=0;
			$this->huo_day30num=0;
		}
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$mounthtime =  mktime(0,0,0,date('m'),1,date('Y'));
		$resdata=array();
		$pindex = max(1, intval($page));
		$psize = 100;
		$total='';
		$userids=pdo_getslice('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'status'=>1),array($pindex,$psize),$total,array('id','userid'),'userid');
		if(count($userids)>0){
				$userids=array_keys($userids);
				$tstart=$daytime-24*3600;
				$tend=$daytime;
				$huo_day0=$this->get_user_count($userids,$tstart,$tend);
				if($huo_day0[0]){
					$this->session('get_huo_day',time());
				}
				foreach($huo_day0 as $hv){
					$this->huo_day0num +=$hv['message_cnt'];
				}
				$tstart=time()-7*24*3600;
				$tend=time();
				$huo_day7=$this->get_user_count($userids,$tstart,$tend);
				foreach($huo_day7 as $hv){
					$this->huo_day7num +=$hv['message_cnt'];
				}
				$tstart=time()-29*24*3600;
				$tend=time();
				$huo_day30=$this->get_user_count($userids,$tstart,$tend);
				foreach($huo_day30 as $hv){
					$this->huo_day30num +=$hv['message_cnt'];
				}
			if(count($userids)>99){
				$page++;
				$this->up_userids_count($page);
			}
		}
		$resdata=array(
			'huo_day7num'=>$this->huo_day7num,
			'huo_day30num'=>$this->huo_day30num,
			'huo_day0num'=>$this->huo_day0num,
		);
		return $resdata;
	}
	public function get_user_count($userids,$start_time=0,$end_time=0){
		$start_time=$start_time?:time()-29*24*3600;
		//$start_time=date('Y-m-d H:i:s',$start_time);
		$end_time=$end_time?:time();
		//$end_time=date('Y-m-d H:i:s',$end_time);
		$params=array(
				'userid'=>$userids,
				'start_time'=>$start_time,
				'end_time'=>$end_time,
		);
		$res=model('core/wxwork_userapi')->get_user_behavior_data($params,true);
		return $res;
	}
	public function member_show($id,$default_url='',$default=false){		
		$member=pdo_get('jy_scrm_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($id)),array('id','name','avatar','alias'));
		if(!$member){
			$member=$default?:array('nickname'=>'无');
		}else{
			$member['nickname']=$member['name']?:'无';
			$member['avatar']=tomedia($member['avatar']);
		}
		return $member;
	}
}
?>